using System;
using System.Data;
using System.Data.SqlClient;
using Smallpointer.Data.Access;
using Smallpointer.Data.MsSql;
using System.Data.OleDb;
using Smallpointer.Data.Oracle;
using System.Data.OracleClient;

namespace Smallpointer.Data.Common
{
    internal class DBExecute : DBase
    {
        internal ScriptUtil Script { get; set; }
        public DBExecute(Data parent)
            : base(parent)
        {
            Script = new ScriptUtil(parent);
        }
        public Output Execute(ExecuteMode mode)
        {
            var temp = new Output();
            Script.Execute(mode);
            var sql = Script.Sql;
            var parameters = Script.Parameters == null || Script.Parameters.Count == 0 ? null : Script.Parameters.ToArray();
            switch (mode)
            {
                case ExecuteMode.Insert:
                    switch (Parent.DBConfig.DBType)
                    {
                        case ConnType.None:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        case ConnType.MsSql:
                            temp.Compeleted = 0 < new MSUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as SqlParameter[]);
                            break;
                        case ConnType.MySql:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        case ConnType.Oracle:
                            temp.Compeleted = 0 < new OracleUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as OracleParameter[]);
                            break;
                        case ConnType.Access:
                            temp.Compeleted = 0 < new AccessUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as OleDbParameter[]);
                            break;
                        case ConnType.Sqlite:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        default:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                    }
                    break;
                case ExecuteMode.Delete:
                    switch (Parent.DBConfig.DBType)
                    {
                        case ConnType.None:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        case ConnType.MsSql:
                            temp.Compeleted = 0 < new MSUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as SqlParameter[]);
                            break;
                        case ConnType.MySql:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        case ConnType.Oracle:
                            temp.Compeleted = 0 < new OracleUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as OracleParameter[]);
                            break;
                        case ConnType.Access:
                            temp.Compeleted = 0 < new AccessUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as OleDbParameter[]);
                            break;
                        case ConnType.Sqlite:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        default:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                    }
                    break;
                case ExecuteMode.Update:
                    switch (Parent.DBConfig.DBType)
                    {
                        case ConnType.None:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        case ConnType.MsSql:
                            temp.Compeleted = 0 < new MSUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as SqlParameter[]);
                            break;
                        case ConnType.MySql:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        case ConnType.Oracle:
                            temp.Compeleted = 0 < new OracleUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as OracleParameter[]);
                            break;
                        case ConnType.Access:
                            temp.Compeleted = 0 < new AccessUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as OleDbParameter[]);
                            break;
                        case ConnType.Sqlite:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        default:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                    }
                    break;
                case ExecuteMode.Select:
                    switch (Parent.DBConfig.DBType)
                    {
                        case ConnType.None:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        case ConnType.MsSql:
                            temp.Merge(new MSUtil(Parent).ExecuteDataSet(CommandType.Text, sql, parameters as SqlParameter[]).Tables[0]);
                            temp.Compeleted = true;
                            break;
                        case ConnType.MySql:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        case ConnType.Oracle:
                            temp.Merge(new OracleUtil(Parent).ExecuteDataSet(CommandType.Text, sql, parameters as OracleParameter[]).Tables[0]);
                            temp.Compeleted = true;
                            break;
                        case ConnType.Access:
                            temp.Merge(new AccessUtil(Parent).ExecuteDataSet(CommandType.Text, sql, parameters as OleDbParameter[]).Tables[0]);
                            temp.Compeleted = true;
                            break;
                        case ConnType.Sqlite:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        default:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                    }
                    break;
                case ExecuteMode.CreateTable:
                    switch (Parent.DBConfig.DBType)
                    {
                        case ConnType.None:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        case ConnType.MsSql:
                            temp.Compeleted = 0 < new MSUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as SqlParameter[]);
                            break;
                        case ConnType.MySql:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        case ConnType.Oracle:
                            temp.Compeleted = 0 < new OracleUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as OracleParameter[]);
                            break;
                        case ConnType.Access:
                            temp.Compeleted = 0 < new AccessUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as OleDbParameter[]);
                            break;
                        case ConnType.Sqlite:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        default:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                    }
                    break;
                case ExecuteMode.DropTable:
                    switch (Parent.DBConfig.DBType)
                    {
                        case ConnType.None:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        case ConnType.MsSql:
                            temp.Compeleted = 0 < new MSUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as SqlParameter[]);
                            break;
                        case ConnType.MySql:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        case ConnType.Oracle:
                            temp.Compeleted = 0 < new OracleUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as OracleParameter[]);
                            break;
                        case ConnType.Access:
                            temp.Compeleted = 0 < new AccessUtil(Parent).ExecuteNonQuery(CommandType.Text, sql, parameters as OleDbParameter[]);
                            break;
                        case ConnType.Sqlite:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                            break;
                        default:
                            throw new DBConcurrencyException("Core does not support database type :" + Parent.DBConfig.DBType);
                    }
                    break;
                default:
                    throw new ArgumentOutOfRangeException("mode", "undefined mode to execute : " + mode);
            }
            temp.Name = Parent.Input.Name;
            return temp;
        }
        public override void Dispose()
        {

        }
    }
}